package com.zenni.utils;

import com.google.common.base.Predicate;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;

/**
 * Created by douglasj on 2/14/16.
 */
public class DriverUtils {
    public static int DEFAULT_WAIT_TIME = 6;

    public boolean isElementExistById(WebDriver driver, String id){
        List<WebElement> e = driver.findElements(By.id(id));
        if(e.size() == 1 ) {
            return true;
        }
        return false;
    }

    public boolean isElementExistsByXpath(WebDriver driver, String xpath){
        List<WebElement> e = driver.findElements(By.xpath(xpath));
        if(e.size() == 1 ) {
            return true;
        }
        return false;
    }

    public boolean isElementExistByLinkText(WebDriver driver, String linkText){
        List<WebElement> e = driver.findElements(By.linkText(linkText));
        if(e.size() == 1 ) {
            return true;
        }
        return false;
    }

    public void waitTillElementExistsById(WebDriver driver, String elementId) {
        waitTillElementExistsById(driver, elementId, DEFAULT_WAIT_TIME);
    }

    public void waitTillElementExistsByLinkText(WebDriver driver, String elementId) {
        waitTillElementExistsByLinkText(driver, elementId, DEFAULT_WAIT_TIME);
    }

    public void waitTillElementExistsByLinkText(WebDriver driver, String elementId, int sleepTime) {
        WebDriverWait wait = new WebDriverWait(driver, sleepTime);
        wait.until(getPredicateForElementExistsByLinkText(elementId));
    }

    public void waitTillElementExistsById(WebDriver driver, String elementId, int sleepTime) {
        WebDriverWait wait = new WebDriverWait(driver, sleepTime);
        wait.until(getPredicateForElementExistsById(elementId));
    }

    public void waitTillElementExistsByXpath(WebDriver driver, String name) {
        waitTillElementExistsByXpath(driver, name, DEFAULT_WAIT_TIME);
    }

    public void waitTillElementExistsByXpath(WebDriver driver, String elementXpath, int sleepTime) {
        WebDriverWait wait = new WebDriverWait(driver, sleepTime);
        wait.until(getPredicateForElementExistsByXpath(elementXpath));
    }

    public void waitTillElementExistsByName(WebDriver driver, String name) {
        waitTillElementExistsById(driver, name, DEFAULT_WAIT_TIME);
    }

    public void waitTillElementExistsByName(WebDriver driver, String name, int sleepTime) {
        WebDriverWait wait = new WebDriverWait(driver, sleepTime);
        wait.until(getPredicateForElementExistsByName(name));
    }

    Predicate<WebDriver> getPredicateForElementExistsById(final String id) {
        return new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver driver) {
                return isElementExistById(driver, id);
            }
        };
    }

    Predicate<WebDriver> getPredicateForElementExistsByLinkText(final String id) {
        return new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver driver) {
                return isElementExistByLinkText(driver, id);
            }
        };
    }

    Predicate<WebDriver> getPredicateForElementExistsByXpath(final String xpath) {
        return new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver driver) {
                return isElementExistsByXpath(driver, xpath);
            }
        };
    }

    Predicate<WebDriver> getPredicateForElementExistsByName(final String name) {
        return new Predicate<WebDriver>() {
            @Override
            public boolean apply(WebDriver driver) {
                return isElementExistByName(driver, name);
            }
        };
    }

    public boolean isElementExistByName(WebDriver driver, String name) {
        List<WebElement> e = driver.findElements(By.name(name));
        if(e.size() == 1 ) {
            return true;
        }
        return false;
    }
}
